<?php

require_once 'google/appengine/api/cloud_storage/CloudStorageTools.php';

use google\appengine\api\cloud_storage\CloudStorageTools;

class RentalController extends \BaseController {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index() {
        $classes = DB::table('classes')->orderBy('class', 'asc')
                        ->distinct()->lists('class', 'id');
        $services = array(
            'Tirane' => 'Tirane',
            'Rinas' => 'Rinas',
        );

        return View::make('pages.new_rental', array(
                    'classes' => $classes,
                    'services' => $services,
        ));
    }

    public function contract_show($id) {
        $data = DB::select('select r.*,r.id as renalId,u.first_name,u.last_name,c.drplace as cplace, '
                        . 'c.picture as cpicture, c.patent_pic as cpatenta_pic, '
                        . 'cr.Make, cr.Model, cr.License, cr.vin '
                        . 'from rentals as r '
                        . 'left join customers as c '
                        . 'on r.drpasportid = c.drpasportid '
                        . 'left join cars as cr on r.vehicle = cr.id '
                        . 'left join users as u on u.id=r.user_id '
                        . 'where r.id = ?', array($id));

        if ($data[0]->cpicture != NULL) {
            $image = CloudStorageTools::getImageServingUrl($data[0]->cpicture, ["secure_url" => true]);
        } else {
            $image = CloudStorageTools::getImageServingUrl('gs://customer_pic/nothumb.jpg', ["secure_url" => true]);
        }
        if ($data[0]->cpatenta_pic != NULL) {
            $patenta = CloudStorageTools::getImageServingUrl($data[0]->cpatenta_pic, ["secure_url" => true]);
        } else {
            $patenta = CloudStorageTools::getImageServingUrl('gs://customer_pic/nothumb.jpg', ["secure_url" => true]);
        }

        return View::make('pages.contract', array(
                    'data' => $data,
                    'image' => $image,
                    'patenta' => $patenta,
        ));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create() {
        $classes = DB::table('classes')->orderBy('class', 'asc')
                        ->distinct()->lists('class', 'id');
        $services = array(
            'Tirane' => 'Tirane',
            'Rinas' => 'Rinas',
        );
        return View::make('pages.new_reservation', array(
                    'classes' => $classes,
                    'services' => $services
        ));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store() {
        Input::flash();
        $data = [
            "errors" => null
        ];
        $attributes = array(
            'drfirstname' => Lang::get('rentals.drfirstname'),
            'drlastname' => Lang::get('rentals.drlastname'),
        );
        $rules = array(
            "drfirstname" => "required",
            "drlastname" => "required",
        );
        $validation = Validator::make(Input::all(), $rules, array(), $attributes);

        if ($validation->passes()) {
            $totalpaid = Input::get('totalpaid');
            $prepaid = Input::get('prepaid');
            $amountdue = floatval($totalpaid - $prepaid);
            $data = new Rental;
            if (Input::get('abroad') === 'yes') {
                $data->abroad = '1';
            } else {
                $data->abroad = '0';
            }
            if (Input::get('payed') === 'yes') {
                $data->rezervationPayed = '1';
            } else {
                $data->rezervationPayed = '0';
            }
            $data->drfirstname = Input::get('drfirstname');
            $data->drlastname = Input::get('drlastname');
            $data->drhphone = Input::get('drhphone');
            $data->cardnr = Input::get('cardnr');
            $data->cardexp = Input::get('cardexp');
            $data->STARTTIME = Input::get('startdate');
            $data->ENDTIME = Input::get('enddate');
            $data->vehicle = Input::get('vehicle');
            $data->status = 'REZERVATION';
            $data->curency = Input::get('curency');
            $data->rentdays = Input::get('rentdays');
            $data->rate = Input::get('rate');
            $data->agent = Input::get('agency');
            $data->prepaid = Input::get('prepaid');
            $data->totalpaid = Input::get('totalpaid');
            $data->amountdue = $amountdue;
            $data->invoice_status = 0;
            $data->user_id = Auth::user()->id;
            $data->save();
            return Response::json(["redirect_to" => "/"]);
        } else {
            if (Request::ajax()) {
                $response_values = array(
                    'validation_failed' => 1,
                    'errors' => $validation->errors()->toArray(),
                );
                return Response::json($response_values);
            } else {
                $data['errors'] = $validation->errors();
                return View::make('pages.test', $data);
            }
        }
    }

    public function rental_store() {
        Input::flash();
        $data = [
            "errors" => null
        ];
        $attributes = array(
            'cfirstname' => Lang::get('rentals.cfirstname'),
            'clastname' => Lang::get('rentals.clastname'),
            'drpasportid' => Lang::get('rentals.drpasportid'),
            'caddress' => Lang::get('rentals.caddress'),
            'drcity' => Lang::get('rentals.drcity'),
            'drcountry' => Lang::get('rentals.drcountry'),
            'drhphone' => Lang::get('rentals.drhphone'),
            'drfirstname' => Lang::get('rentals.drfirstname'),
            'drlastname' => Lang::get('rentals.drlastname'),
            'drbdate' => Lang::get('rentals.drbdate'),
            'drlexpdate' => Lang::get('rentals.drlexpdate'),
            'drliccat' => Lang::get('rentals.drliccat'),
            'drlicnr' => Lang::get('rentals.drlicnr'),
        );
        $rules = array(
            "cfirstname" => "required",
            "clastname" => "required",
            "drpasportid" => "required|unique:customers,drpasportid",
            "caddress" => "required",
            "drcity" => "required",
            "drcountry" => "required",
            "drhphone" => "required",
            "drfirstname" => "required",
            "drlastname" => "required",
            "drbdate" => "required",
            "drlexpdate" => "required",
            "drliccat" => "required",
            "drlicnr" => "required",
        );
        $validation = Validator::make(Input::all(), $rules, array(), $attributes);

        if ($validation->passes()) {

            $data = new Rental;
            $data->STARTTIME = Input::get('startdate');
            $data->ENDTIME = Input::get('enddate');
            $data->vehicle = Input::get('vehicle');
            $data->drfirstname = Input::get('drfirstname');
            $data->drlastname = Input::get('drlastname');
            $data->drpasportid = Input::get('drpasportid');
            $data->drbdate = Input::get('drbdate');
            $data->drlexpdate = Input::get('drlexpdate');
            $data->drliccat = Input::get('drliccat');
            $data->drlicnr = Input::get('drlicnr');
            $data->dr2fname = Input::get('dr2fname');
            $data->dr2lastname = Input::get('dr2lastname');
            $data->draddress = Input::get('draddress');
            $data->dr2bdate = Input::get('dr2bdate');
            $data->dr2licexpdate = Input::get('dr2licexpdate');
            $data->dr2liccat = Input::get('dr2liccat');
            $data->dr2licnr = Input::get('dr2licnr');
            $data->dr2licexpdate = Input::get('dr2licexpdate');
            $data->dr2city = Input::get('dr2city');
            $data->dr2country = Input::get('dr2country');
            $data->fuelin = Input::get('fuelin');
            $data->kmin = Input::get('kmin');
            $data->rentdays = Input::get('nrdays');
            $data->totalpaid = Input::get('result');
            $data->rate = Input::get('rate');
            $data->curency = Input::get('curency');
            $data->agent = Input::get('agency');
            $data->drpasportid = Input::get('drpasportid');
            $data->invoice_status = 0;
            $data->status = 'CHECK_OUT';
            $data->user_id = Auth::user()->id;
            $data->save();

            $data2 = new Customer;
            $data2->drfirstname = Input::get('cfirstname');
            $data2->drlastname = Input::get('clastname');
            $data2->drpasportid = Input::get('drpasportid');
            $data2->draddress = Input::get('caddress');
            $data2->drcity = Input::get('drcity');
            $data2->drcountry = Input::get('drcountry');
            $data2->dremail = Input::get('dremail');
            $data2->drhphone = Input::get('drhphone');
            $data2->workphone = Input::get('workphone');
            $data2->companyname = Input::get('companyname');
            $data2->drbdate = Input::get('drbdate');
            $data2->drlexpdate = Input::get('drlexpdate');
            $data2->drliccat = Input::get('drliccat');
            $data2->drlicnr = Input::get('drlicnr');
            $data2->save();

            $data3 = new Car;
            $data3->Milage = Input::get('milage');
            $data3->fuel = Input::get('fuelin');
            $data3->save();

            $insert_id = $data->id;
            $drpasportId = Input::get('drpasportid');
            return Response::json(["redirect_to" => "../../upload_patent_rental/" . $drpasportId . '/' . $insert_id]);
        } else {
            if (Request::ajax()) {
                $response_values = array(
                    'validation_failed' => 1,
                    'errors' => $validation->errors()->toArray(),
                );
                return Response::json($response_values);
            } else {
                $data['errors'] = $validation->errors();
                return View::make('pages.rental_create', $data);
            }
        }
    }

    public function customer_rental_save() {
        Input::flash();
        $data = [
            "errors" => null
        ];
        $attributes = array(
            'cfirstname' => Lang::get('rentals.cfirstname'),
            'clastname' => Lang::get('rentals.clastname'),
            'drpasportid' => Lang::get('rentals.drpasportid'),
            'caddress' => Lang::get('rentals.caddress'),
            'drcity' => Lang::get('rentals.drcity'),
            'drcountry' => Lang::get('rentals.drcountry'),
            'drhphone' => Lang::get('rentals.drhphone'),
            'drfirstname' => Lang::get('rentals.drfirstname'),
            'drlastname' => Lang::get('rentals.drlastname'),
            'drbdate' => Lang::get('rentals.drbdate'),
            'drlexpdate' => Lang::get('rentals.drlexpdate'),
            'drliccat' => Lang::get('rentals.drliccat'),
            'drlicnr' => Lang::get('rentals.drlicnr'),
        );
        $rules = array(
            "cfirstname" => "required",
            "clastname" => "required",
            "drpasportid" => "required",
            "caddress" => "required",
            "drcity" => "required",
            "drcountry" => "required",
            "drhphone" => "required",
            "drfirstname" => "required",
            "drlastname" => "required",
            "drbdate" => "required",
            "drlexpdate" => "required",
            "drliccat" => "required",
            "drlicnr" => "required",
        );
        $validation = Validator::make(Input::all(), $rules, array(), $attributes);

        if ($validation->passes()) {

            $data = new Rental;
            $data->starttime = Input::get('startdate');
            $data->endtime = Input::get('enddate');
            $data->vehicle = Input::get('vehicle');
            $data->drfirstname = Input::get('drfirstname');
            $data->drlastname = Input::get('drlastname');
            $data->drpasportid = Input::get('drpasportid');
            $data->drbdate = Input::get('drbdate');
            $data->drlexpdate = Input::get('drlexpdate');
            $data->drliccat = Input::get('drliccat');
            $data->drlicnr = Input::get('drlicnr');
            $data->dr2fname = Input::get('dr2fname');
            $data->dr2lastname = Input::get('dr2lastname');
            $data->draddress = Input::get('draddress');
            $data->dr2bdate = Input::get('dr2bdate');
            $data->dr2licexpdate = Input::get('dr2licexpdate');
            $data->dr2liccat = Input::get('dr2liccat');
            $data->dr2licnr = Input::get('dr2licnr');
            $data->dr2licexpdate = Input::get('dr2licexpdate');
            $data->dr2city = Input::get('dr2city');
            $data->dr2country = Input::get('dr2country');
            $data->fuelin = Input::get('fuelin');
            $data->kmin = Input::get('kmin');
            $data->rentdays = Input::get('nrdays');
            $data->totalpaid = Input::get('result');
            $data->rate = Input::get('rate');
            $data->curency = Input::get('curency');
            $data->agent = Input::get('agency');
            $data->drpasportid = Input::get('drpasportid');
            $data->status = 'CHECK_OUT';
            $data->invoice_status = 0;
            $data->user_id = Auth::user()->id;
            $data->save();

            $pasportid = Input::get('drpasportid');
            $data2=Customer::find($pasportid);
            $data2->drfirstname = Input::get('cfirstname');
            $data2->drlastname = Input::get('clastname');
            $data2->draddress = Input::get('caddress');
            $data2->drcity = Input::get('drcity');
            $data2->drcountry = Input::get('drcountry');
            $data2->dremail = Input::get('dremail');
            $data2->drhphone = Input::get('drhphone');
            $data2->workphone = Input::get('workphone');
            $data2->companyname = Input::get('companyname');
            $data2->drbdate = Input::get('drbdate');
            $data2->drlexpdate = Input::get('drlexpdate');
            $data2->drliccat = Input::get('drliccat');
            $data2->drlicnr = Input::get('drlicnr');
            $data2->save();

            $carid = Input::get('vehicle');
            $data3 = Car::find($carid);
            $data3->Milage = Input::get('milage');
            $data3->fuel = Input::get('fuelin');
            $data3->save();

            $insert_id = $data->id;
            $drpasportId = Input::get('drpasportid');
            return Response::json(["redirect_to" => "../../upload_patent_rental/" . $drpasportId . '/' . $insert_id]);
            // return Response::json(["redirect_to" => "../../rental_invoice/" . $insert_id]);
        } else {
            if (Request::ajax()) {
                $response_values = array(
                    'validation_failed' => 1,
                    'errors' => $validation->errors()->toArray(),
                );
                return Response::json($response_values);
            } else {
                $data['errors'] = $validation->errors();
                return View::make('pages.customer_new_rental', $data);
            }
        }
    }

    public function customer_rental_store() {
        Input::flash();
        $data = [
            "errors" => null
        ];
        $attributes = array(
            'drfirstname' => Lang::get('rentals.drfirstname'),
            'drlastname' => Lang::get('rentals.drlastname'),
        );
        $rules = array(
            "drfirstname" => "required",
            "drlastname" => "required",
        );
        $validation = Validator::make(Input::all(), $rules, array(), $attributes);

        if ($validation->passes()) {

            $data = new Rental;
            if (Input::get('abroad') === 'yes') {
                $data->abroad = '1';
            } else {
                $data->abroad = '0';
            }
            if (Input::get('payed') === 'yes') {
                $data->payed = '1';
            } else {
                $data->payed = '0';
            }
            $data->drfirstname = Input::get('drfirstname');
            $data->drlastname = Input::get('drlastname');
            $data->drhphone = Input::get('drhphone');
            $data->cardnr = Input::get('cardnr');
            $data->cardexp = Input::get('cardexp');
            $data->STARTTIME = Input::get('startdate');
            $data->ENDTIME = Input::get('enddate');
            $data->vehicle = Input::get('vehicle');
            $data->status = 'REZERVATION';
            $data->rezervationPayed = Input::get('amountpaid');
            $data->curency = Input::get('curency');
            $data->rentdays = Input::get('rentdays');
            $data->rezervationAmount = Input::get('amount');
            $data->rate = Input::get('rate');
            $data->agent = Input::get('agency');
            $data->user_id = Auth::user()->id;
            $data->invoice_status = 0;
            $data->save();

            return Response::json(["redirect_to" => "/"]);
        } else {
            if (Request::ajax()) {
                $response_values = array(
                    'validation_failed' => 1,
                    'errors' => $validation->errors()->toArray(),
                );
                return Response::json($response_values);
            } else {
                $data['errors'] = $validation->errors();
                return View::make('pages.test', $data);
            }
        }
    }

    public function customer_save($id) {
        Input::flash();
        $data = [
            "errors" => null
        ];
        $attributes = array(
            'cfirstname' => Lang::get('rentals.cfirstname'),
            'clastname' => Lang::get('rentals.clastname'),
            'drpasportid' => Lang::get('rentals.drpasportid'),
            'caddress' => Lang::get('rentals.caddress'),
            'drcity' => Lang::get('rentals.drcity'),
            'drcountry' => Lang::get('rentals.drcountry'),
            'drhphone' => Lang::get('rentals.drhphone'),
            'drfirstname' => Lang::get('rentals.drfirstname'),
            'drlastname' => Lang::get('rentals.drlastname'),
            'drbdate' => Lang::get('rentals.drbdate'),
            'drlexpdate' => Lang::get('rentals.drlexpdate'),
            'drliccat' => Lang::get('rentals.drliccat'),
            'drlicnr' => Lang::get('rentals.drlicnr'),
        );
        $rules = array(
            "cfirstname" => "required",
            "clastname" => "required",
            "drpasportid" => "required",
            "caddress" => "required",
            "drcity" => "required",
            "drcountry" => "required",
            "drhphone" => "required",
            "drfirstname" => "required",
            "drlastname" => "required",
            "drbdate" => "required",
            "drlexpdate" => "required",
            "drliccat" => "required",
            "drlicnr" => "required",
        );
        $validation = Validator::make(Input::all(), $rules, array(), $attributes);

        if ($validation->passes()) {

            $data = Rental::find($id);
            $data->vehicle = Input::get('vehicle');
            $data->drfirstname = Input::get('drfirstname');
            $data->drlastname = Input::get('drlastname');
            $data->drpasportid = Input::get('drpasportid');
            $data->drbdate = Input::get('drbdate');
            $data->drlexpdate = Input::get('drlexpdate');
            $data->drliccat = Input::get('drliccat');
            $data->drlicnr = Input::get('drlicnr');
            $data->dr2fname = Input::get('dr2fname');
            $data->dr2lastname = Input::get('dr2lastname');
            $data->draddress = Input::get('draddress');
            $data->dr2bdate = Input::get('dr2bdate');
            $data->dr2licexpdate = Input::get('dr2licexpdate');
            $data->dr2liccat = Input::get('dr2liccat');
            $data->dr2licnr = Input::get('dr2licnr');
            $data->dr2licexpdate = Input::get('dr2licexpdate');
            $data->dr2city = Input::get('dr2city');
            $data->dr2country = Input::get('dr2country');
            $data->fuelin = Input::get('fuelin');
            $data->kmin = Input::get('kmin');
            $data->rentdays = Input::get('nrdays');
            $data->totalpaid = Input::get('result');
            $data->rate = Input::get('rate');
            $data->curency = Input::get('curency');
            $data->agent = Input::get('agency');
            $data->drpasportid = Input::get('drpasportid');
            $data->status = 'CHECK_OUT';
            $data->invoice_status = 0;
            $data->user_id = Auth::user()->id;
            $data->save();

            $pasportid = Input::get('drpasportid');
            $data2=Customer::find($pasportid);
            $data2->drfirstname = Input::get('cfirstname');
            $data2->drlastname = Input::get('clastname');
            $data2->draddress = Input::get('caddress');
            $data2->drcity = Input::get('drcity');
            $data2->drcountry = Input::get('drcountry');
            $data2->dremail = Input::get('dremail');
            $data2->drhphone = Input::get('drhphone');
            $data2->workphone = Input::get('workphone');
            $data2->companyname = Input::get('companyname');
            $data2->drbdate = Input::get('drbdate');
            $data2->drlexpdate = Input::get('drlexpdate');
            $data2->drliccat = Input::get('drliccat');
            $data2->drlicnr = Input::get('drlicnr');
            $data2->save();

            $carid = Input::get('vehicle');
            $data3 = Car::find($carid);
            $data3->Milage = Input::get('milage');
            $data3->fuel = Input::get('fuelin');
            $data3->save();
            $drpasportId = Input::get('drpasportid');
            return Response::json(["redirect_to" => "../../upload_patent_rental/" . $drpasportId . '/' . $id]);
            //return Response::json(["redirect_to" => "../../rental_invoice/" . $id]);
        } else {
            if (Request::ajax()) {
                $response_values = array(
                    'validation_failed' => 1,
                    'errors' => $validation->errors()->toArray(),
                );
                return Response::json($response_values);
            } else {
                $data['errors'] = $validation->errors();
                return View::make('pages.customer_rental', $data);
            }
        }
    }

    public function rental_payment($id) {
        $data = Rental::find($id);
        $data->payment_status = 1;
        $data->save();
        return Redirect::to('rental_invoice/' . $id);
    }

    public function rental_invoice($id) {

        $data = DB::select('select r.*,r.id as rentalId, c.drfirstname as cfname, '
                        . 'c.drlastname as clname, '
                        . 'cr.Make, cr.Model, cr.License, cr.vin '
                        . 'from rentals as r '
                        . 'left join customers as c '
                        . 'on c.drpasportid=r.drpasportid '
                        . 'left join cars as cr on cr.id=r.vehicle '
                        . 'where r.id = ?', array($id));

        return View::make('pages.invoice', array(
                    'data' => $data,
                    'invoice_id' => $data[0]->id,
                    'invoice_date' => $data[0]->created_at,
                    'due_date' => $data[0]->updated_at,
                    'qty' => $data[0]->rentdays,
                    'price' => $data[0]->rate,
                    'make' => $data[0]->Make,
                    'model' => $data[0]->Model,
                    'amount' => $data[0]->totalpaid,
                    'amountdue' => $data[0]->amountdue,
                    'prepaid' => $data[0]->prepaid,
                    'vehicle_id' => $data[0]->vehicle,
                    'cfname' => $data[0]->cfname,
                    'clname' => $data[0]->clname,
                    'curency' => $data[0]->curency,
                    'startdate' => $data[0]->STARTTIME,
                    'enddate' => $data[0]->ENDTIME,
                    'status' => $data[0]->invoice_status
        ));
    }

    public function show_freeCars() {
        Input::flash();
        $data = [
            "errors" => null
        ];
        $attributes = array(
            'startdate' => Lang::get('rentals.startdate'),
            'enddate' => Lang::get('rentals.enddate'),
        );
        $rules = array(
            "startdate" => "required",
            "enddate" => "required",
        );
        $validation = Validator::make(Input::all(), $rules, array(), $attributes);

        $starttime = Input::get('starttime');
        $startdate = Input::get('startdate');
        $classes = Input::get('classes');


        if ($classes != 11) {
            $free_cars = DB::select('SELECT * FROM cars WHERE '
                            . 'id NOT IN (select distinct c.id from cars as c '
                            . 'left join rentals as r on r.vehicle=c.id '
                            . 'where r.STARTTIME>="' . $startdate . ' ' . $starttime . '" '
                            . 'AND (r.status="REZERVATION" OR r.status="CHECK_OUT") '
                            . 'order by c.id) AND class= "' . $classes . '" ');
        } else {
            $free_cars = DB::select('SELECT * FROM cars WHERE '
                            . 'id NOT IN (select distinct c.id from cars as c '
                            . 'left join rentals as r on r.vehicle=c.id '
                            . 'where r.STARTTIME>="' . $startdate . ' ' . $starttime . '" '
                            . 'AND (r.status="REZERVATION" OR r.status="CHECK_OUT") '
                            . 'order by c.id)');
        }

        if (Request::ajax()) {
            $response_values = array(
                'success' => 1,
                'values' => $free_cars,
                'errors' => $validation->errors()->toArray(),
            );
            return Response::json($response_values);
        } else {
            dd('error');
        }
    }

    public function reservation_create($id) {
        $values = explode('~', $id);
        $datediff = strtotime($values[2]) - strtotime($values[1]);

        $nrdays = (int) ($datediff / (60 * 60 * 24));
        $currencies = array(
            'EUR' => 'EUR',
            'LEK' => 'LEK',
        );
        $car = DB::select('select c.*, cl.class as class_name  '
                        . 'from cars as c '
                        . 'left join classes as cl '
                        . 'on c.class = cl.id '
                        . 'where c.id = "' . $values[0] . '"');
        if ($nrdays >= 1 && $nrdays <= 2) {
            $result = $nrdays * $car[0]->drate2;
            return View::make('pages.reservation_create', array(
                        'vehicle' => $values[0],
                        'startdate' => $values[1],
                        'enddate' => $values[2],
                        'car' => $car,
                        'nrdays' => $nrdays,
                        'rate' => $car[0]->drate2,
                        'result' => $result,
                        'currencies' => $currencies
            ));
        } elseif ($nrdays >= 3 && $nrdays <= 6) {
            $result = $nrdays * $car[0]->drate3;
            return View::make('pages.reservation_create', array(
                        'vehicle' => $values[0],
                        'startdate' => $values[1],
                        'enddate' => $values[2],
                        'car' => $car,
                        'nrdays' => $nrdays,
                        'rate' => $car[0]->drate3,
                        'result' => $result,
                        'currencies' => $currencies
            ));
        } elseif ($nrdays >= 7 && $nrdays <= 11) {
            $result = $nrdays * $car[0]->drate4;
            return View::make('pages.reservation_create', array(
                        'vehicle' => $values[0],
                        'startdate' => $values[1],
                        'enddate' => $values[2],
                        'car' => $car,
                        'nrdays' => $nrdays,
                        'rate' => $car[0]->drate4,
                        'result' => $result,
                        'currencies' => $currencies
            ));
        } elseif ($nrdays >= 12 && $nrdays <= 25) {
            $result = $nrdays * $car[0]->drate5;
            return View::make('pages.reservation_create', array(
                        'vehicle' => $values[0],
                        'startdate' => $values[1],
                        'enddate' => $values[2],
                        'car' => $car,
                        'nrdays' => $nrdays,
                        'rate' => $car[0]->drate5,
                        'result' => $result,
                        'currencies' => $currencies
            ));
        } elseif ($nrdays >= 26 && $nrdays <= 35) {
            $result = $nrdays * $car[0]->drate6;
            return View::make('pages.reservation_create', array(
                        'vehicle' => $values[0],
                        'startdate' => $values[1],
                        'enddate' => $values[2],
                        'car' => $car,
                        'nrdays' => $nrdays,
                        'rate' => $car[0]->drate6,
                        'result' => $result,
                        'currencies' => $currencies
            ));
        }
    }

    public function rental_create($id) {

        $values = explode('~', $id);
        $startdate = $values[1] . ' ' . $values[2];

        $enddate = $values[3] . ' ' . $values[4];
        $datediff = strtotime($enddate) - strtotime($startdate);

        $nrdays = (int) ($datediff / (60 * 60 * 24));

        $currencies = array(
            'EUR' => 'EUR',
            'LEK' => 'LEK',
        );
        $car = DB::select('select c.*, cl.class as class_name  '
                        . 'from cars as c '
                        . 'left join classes as cl '
                        . 'on c.class = cl.id '
                        . 'where c.id = "' . $values[0] . '"');

        if ($nrdays >= 1 && $nrdays <= 2) {
            $result = $nrdays * $car[0]->drate2;
            return View::make('pages.rental_create', array(
                        'vehicle' => $values[0],
                        'startdate' => $startdate,
                        'enddate' => $enddate,
                        'car' => $car,
                        'nrdays' => $nrdays,
                        'rate' => $car [0]->drate2,
                        'result' => $result,
                        'currencies' => $currencies,
            ));
        } elseif ($nrdays >= 3 && $nrdays <= 6) {
            $result = $nrdays * $car[0]->drate3;
            return View::make('pages.rental_create', array(
                        'vehicle' => $values[0],
                        'startdate' => $startdate,
                        'enddate' => $enddate,
                        'car' => $car,
                        'nrdays' => $nrdays,
                        'rate' => $car[0]->drate3,
                        'result' => $result,
                        'currencies' => $currencies,
            ));
        } elseif ($nrdays >= 7 && $nrdays <= 11) {
            $result = $nrdays * $car[0]->drate4;
            return View::make('pages.rental_create', array(
                        'vehicle' => $values [0],
                        'startdate' => $startdate,
                        'enddate' => $enddate,
                        'car' => $car,
                        'nrdays' => $nrdays,
                        'rate' => $car[0]->drate4,
                        'result' => $result,
                        'currencies' => $currencies,
            ));
        } elseif ($nrdays >= 12 && $nrdays <= 25) {
            $result = $nrdays * $car[0]->drate5;
            return View::make('pages.rental_create', array(
                        'vehicle' => $values[0],
                        'startdate' => $startdate,
                        'enddate' => $enddate,
                        'car' => $car,
                        'nrdays' => $nrdays,
                        'rate' => $car[0]->drate5,
                        'result' => $result,
                        'currencies' => $currencies,
            ));
        } elseif ($nrdays >= 26 && $nrdays <= 35) {
            $result = $nrdays * $car[0]->drate6;
            return View::make('pages.rental_create', array(
                        'vehicle' => $values[0],
                        'startdate' => $startdate,
                        'enddate' => $enddate,
                        'car' => $car,
                        'nrdays' => $nrdays,
                        'rate' => $car[0]->drate6,
                        'result' => $result,
                        'currencies' => $currencies,
            ));
        }
    }

    public function show_reservation() {
        $starttime = Input::get('starttime');
        $startdate = Input::get('startdate');
        $endtime = Input::get('endtime');
        $enddate = Input::get('enddate');
        $classes = Input::get('classes');
        $today = date('Y-m-d H:i:s');

        if ($classes != 11 && $startdate != '') {

            $results = DB::select('select r.*, r.id as rentalId, r.prepaid as res_prepaid, r.rentdays as res_rentdays, c.*,o.customerID, o.rentalID, oc.*, ob.* from rentals as r '
                            . 'left join cars as c '
                            . 'on r.vehicle = c.id '
                            . 'left join online_reservation as o '
                            . 'on o.resID = r.onlineResId '
                            . 'left join online_customers as oc on oc.cusID = o.customerID '
                            . 'left join online_booking as ob on ob.bookID = o.bookID '
                            . 'WHERE r.STARTTIME >= "' . $startdate . '" '
                            . 'AND c.class = "' . $classes . '" '
                            . 'AND r.status = "REZERVATION"');
        } elseif ($starttime == '' && $startdate == '' && $endtime == '' && $enddate == '' && $classes == 11) {

            $results = DB::select('select r.*, r.id as rentalId,r.prepaid as res_prepaid,r.rentdays as res_rentdays, c.*, o.customerID, o.rentalID, oc.*, ob.* from rentals as r '
                            . 'left join cars as c '
                            . 'on c.id = r.vehicle  '
                            . 'left join online_reservation as o '
                            . 'on o.resID = r.onlineResId '
                            . 'left join online_customers as oc on oc.cusID = o.customerID '
                            . 'left join online_booking as ob on ob.bookID = o.bookID '
                            . 'WHERE r.STARTTIME >= "' . $today . '" '
                            . 'AND r.status = "REZERVATION"');
        } elseif ($starttime == '' && $startdate == '' && $endtime == '' && $enddate == '' && $classes != 11) {

            $results = DB::select('select r.*, r.id as rentalId,r.prepaid as res_prepaid,r.rentdays as res_rentdays, c.*,o.customerID, o.rentalID, oc.*, ob.* from rentals as r '
                            . 'left join cars as c '
                            . 'on c.id=r.vehicle '
                            . 'left join online_reservation as o '
                            . 'on o.resID = r.onlineResId '
                            . 'left join online_customers as oc on oc.cusID = o.customerID '
                            . 'left join online_booking as ob on ob.bookID = o.bookID '
                            . 'WHERE r.STARTTIME >= "' . $today . '" '
                            . 'AND c.class = "' . $classes . '" '
                            . 'AND r.status = "REZERVATION"');
        } else {

            $results = DB::select('select r.*, r.id as rentalId,r.prepaid as res_prepaid,r.rentdays as res_rentdays, c.*, from rentals as r '
                            . 'left join cars as c '
                            . 'on  c.id = r.vehicle '
                            . 'left join online_reservation as o '
                            . 'on o.resID = r.onlineResId '
                            . 'left join online_customers as oc on oc.cusID = o.customerID '
                            . 'left join online_booking as ob on ob.bookID = o.bookID '
                            . 'WHERE r.STARTTIME >= "' . $startdate . ' ' . $starttime . '" '
                            . 'AND r.status = "REZERVATION"');
        }
        if (Request::ajax()) {
            $response_values = array(
                'success' => 1,
                'values' => $results,
            );
            return Response::json($response_values);
        } else {
            dd('error');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id) {
//
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id) {
        $data = explode('~', $id);

        $currencies = array(
            'EUR' => 'EUR',
            'LEK' => 'LEK',
        );
        $values = DB::select('select r.*,r.id as rentalId,r.rate as rate_value,cl.class as class_name, '
                        . 'c.* from rentals as r '
                        . 'left join cars as c on c.id=r.vehicle '
                        . 'left join classes as cl on cl.id=c.class '
                        . 'where r.id = ?', array($data[0]));

        $result = $values[0]->rentdays * $values[0]->rate_value;

        return View::make('pages.reservation_edit', array(
                    'values' => $values,
                    'currencies' => $currencies,
                    'result' => $result
        ));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id) {
        Input::flash();
        $data = [
            "errors" => null
        ];
        $attributes = array(
            'cfirstname' => Lang::get('rentals.cfirstname'),
            'clastname' => Lang::get('rentals.clastname'),
            'drpasportid' => Lang::get('rentals.drpasportid'),
            'caddress' => Lang::get('rentals.caddress'),
            'drcity' => Lang::get('rentals.drcity'),
            'drcountry' => Lang::get('rentals.drcountry'),
            'drhphone' => Lang::get('rentals.drhphone'),
            'drfirstname' => Lang::get('rentals.drfirstname'),
            'drlastname' => Lang::get('rentals.drlastname'),
            'drbdate' => Lang::get('rentals.drbdate'),
            'drlexpdate' => Lang::get('rentals.drlexpdate'),
            'drliccat' => Lang::get('rentals.drliccat'),
            'drlicnr' => Lang::get('rentals.drlicnr'),
        );
        $rules = array(
            "cfirstname" => "required",
            "clastname" => "required",
            "drpasportid" => "required|unique:customers,drpasportid",
            "caddress" => "required",
            "drcity" => "required",
            "drcountry" => "required",
            "drhphone" => "required",
            "drfirstname" => "required",
            "drlastname" => "required",
            "drbdate" => "required",
            "drlexpdate" => "required",
            "drliccat" => "required",
            "drlicnr" => "required",
        );
        $validation = Validator::make(Input::all(), $rules, array(), $attributes);
        if ($validation->passes()) {

            $data = Rental::find($id);

            $data->vehicle = Input::get('vehicle');
            $data->drfirstname = Input::get('drfirstname');
            $data->drlastname = Input::get('drlastname');
            $data->drbdate = Input::get('drbdate');
            $data->drlexpdate = Input::get('drlexpdate');
            $data->drliccat = Input::get('drliccat');
            $data->drlicnr = Input::get('drlicnr');
            $data->dr2fname = Input::get('dr2fname');
            $data->dr2lastname = Input::get('dr2lastname');
            $data->draddress = Input::get('draddress');
            $data->dr2bdate = Input::get('dr2bdate');
            $data->dr2licexpdate = Input::get('dr2licexpdate');
            $data->dr2liccat = Input::get('dr2liccat');
            $data->dr2licnr = Input::get('dr2licnr');
            $data->dr2licexpdate = Input::get('dr2licexpdate');
            $data->dr2city = Input::get('dr2city');
            $data->dr2country = Input::get('dr2country');
            $data->fuelin = Input::get('fuelin');
            $data->kmin = Input::get('kmin');
            $data->rentdays = Input::get('nrdays');
            $data->totalpaid = Input::get('result');
            $data->rate = Input::get('rate');
            $data->curency = Input::get('curency');
            $data->agent = Input::get('agency');
            $data->prepaid = Input::get('gurantee');
            $data->curencyg = Input::get('curencyg');
            $data->drpasportid = Input::get('drpasportid');
            $data->status = 'CHECK_OUT';
            $data->invoice_status = 0;

            $data->user_id = Auth::user()->id;
            $data->save();

            $data2 = New Customer;
            $data2->drfirstname = Input::get('cfirstname');
            $data2->drlastname = Input::get('clastname');
            $data2->drpasportid = Input::get('drpasportid');
            $data2->draddress = Input::get('caddress');
            $data2->drcity = Input::get('drcity');
            $data2->drcountry = Input::get('drcountry');
            $data2->dremail = Input::get('dremail');
            $data2->drhphone = Input::get('drhphone');
            $data2->workphone = Input::get('workphone');
            $data2->companyname = Input::get('companyname');
            $data2->drbdate = Input::get('drbdate');
            $data2->drlexpdate = Input::get('drlexpdate');
            $data2->drliccat = Input::get('drliccat');
            $data2->drlicnr = Input::get('drlicnr');
            $data2->save();

            $carid = Input::get('vehicle');
            $data3 = Car::find($carid);
            $data3->Milage = Input::get('milage');
            $data3->fuel = Input::get('fuelin');
            $data3->save();

            $drpasportId = Input::get('drpasportid');
            return Response::json(["redirect_to" => "../../upload_patent_rental/" . $drpasportId . '/' . $id]);
            // return Response::json(["redirect_to" => "../../rental_invoice/" . $id]);
        } else {
            if (Request::ajax()) {
                $response_values = array(
                    'validation_failed' => 1,
                    'errors' => $validation->errors()->toArray(),
                );
                return Response::json($response_values);
            } else {
                $data['errors'] = $validation->errors();
                return View::make('pages.reservation_edit', $data);
            }
        }
    }

    public function all_rentals() {
        $results = DB::select('select r.*,r.id as rentalId,cr.id, cr.Make, cr.Model '
                        . 'from rentals as r left join customers as c '
                        . 'on c.drpasportid= r.drpasportid left join cars as cr on cr.id=r.vehicle '
                        . 'where r.status="CHECK_OUT" ');

        return View::make('pages.all_rentals', array(
                    'results' => $results
        ));
    }

    public function all_reservations() {
        $results = DB::select('select r.*,r.id as rentalId, cr.id, cr.Make, cr.Model '
                        . 'from rentals as r left join customers as c '
                        . 'on c.drpasportid= r.drpasportid left join cars as cr on cr.id=r.vehicle '
                        . 'where r.status="REZERVATION"');

        return View::make('pages.all_reservations', array(
                    'results' => $results
        ));
    }

    public function existing_customers() {
        $customers = DB::select('select * from customers');
        return json_encode($customers);
    }

    public function customer_rental($id) {
        $data = json_decode($id);
        $drfirstname = $data[0];
        $drlastname = $data[1];
        $drpasportid = $data[2];
        $draddress = $data[3];
        $drcity = $data[4];
        $drcountry = $data[5];
        $DREMAIL = $data[6];
        $drhphone = $data[7];
        $workphone = $data[8];
        $companyname = $data[9];
        $dob = $data[10];
        $drliccat = $data[11];
        $nrdays = $data[12];
        $result = $data[13];
        $rate = $data[14];
        $drlexpdate = $data[16];
        $drlicnr = $data[17];
        $rentalId = $data[18];

        $vehicle = DB::select('select c.*, cl.class as class_name from cars as c '
                        . 'left join classes as cl on cl.id=c.class '
                        . 'where c.id=' . $data[15] . '');

        $currencies = array(
             'EUR' => 'EUR',
            'LEK' => 'LEK',
        );

        return View::make('pages.customer_rental', array(
                    'cfirstname' => $drfirstname,
                    'clastname' => $drlastname,
                    'drpasportid' => $drpasportid,
                    'caddress' => $draddress,
                    'drcity' => $drcity,
                    'drcountry' => $drcountry,
                    'dremail' => $DREMAIL,
                    'drhphone' => $drhphone,
                    'workphone' => $workphone,
                    'companyname' => $companyname,
                    'dob' => $dob,
                    'drliccat' => $drliccat,
                    'currencies' => $currencies,
                    'nrdays' => $nrdays,
                    'result' => $result,
                    'rate' => $rate,
                    'vehicle' => $vehicle,
                    'drlexpdate' => $drlexpdate,
                    'drlicnr' => $drlicnr,
                    'rentalId' => $rentalId
        ));
    }

    public function customer_new_rental($id) {
        $data = json_decode($id);
        $drfirstname = $data[0];
        $drlastname = $data[1];
        $drpasportid = $data[2];
        $draddress = $data[3];
        $drcity = $data[4];
        $drcountry = $data[5];
        $DREMAIL = $data[6];
        $drhphone = $data[7];
        $workphone = $data[8];
        $companyname = $data[9];
        $dob = $data[10];
        $drliccat = $data[11];
        $nrdays = $data[12];
        $result = $data[13];
        $rate = $data[14];
        $drlexpdate = $data[16];
        $drlicnr = $data[17];
        $startdate = $data[18];
        $enddate = $data[19];

        $vehicle = DB::select('select c.*, cl.class as class_name from cars as c '
                        . 'left join classes as cl on cl.id=c.class '
                        . 'where c.id=' . $data[15] . '');

        $currencies = array(
             'EUR' => 'EUR',
            'LEK' => 'LEK',
        );

        return View::make('pages.customer_new_rental', array(
                    'cfirstname' => $drfirstname,
                    'clastname' => $drlastname,
                    'drpasportid' => $drpasportid,
                    'caddress' => $draddress,
                    'drcity' => $drcity,
                    'drcountry' => $drcountry,
                    'dremail' => $DREMAIL,
                    'drhphone' => $drhphone,
                    'workphone' => $workphone,
                    'companyname' => $companyname,
                    'dob' => $dob,
                    'drliccat' => $drliccat,
                    'currencies' => $currencies,
                    'nrdays' => $nrdays,
                    'result' => $result,
                    'rate' => $rate,
                    'vehicle' => $vehicle,
                    'drlexpdate' => $drlexpdate,
                    'drlicnr' => $drlicnr,
                    'startdate' => $startdate,
                    'enddate' => $enddate
        ));
    }

    public function check_in_search() {
        $values = DB::select('select r.*, r.id as rentalId, c.drfirstname as cfname,'
                        . 'c.drlastname as clname,c.drcity as ccity, cr.* from rentals as r '
                        . 'left join customers as c '
                        . 'on c.drpasportid=r.drpasportid '
                        . 'left join cars as cr on cr.id=r.vehicle '
                        . 'where r.status = "CHECK_OUT"');

        return View::make('pages.check_in_search', array(
                    'values' => $values,
        ));
    }

    public function check_in($id) {

        $values = DB::select('select r.*, r.id as rentalId, r.rate as rate_value, c.drfirstname as cfname,'
                        . 'c.drlastname as clname,c.drcity as ccity,c.draddress as caddress, '
                        . 'c.drcountry as ccountry,c.DREMAIL as cemail, c.drhphone as chphone,'
                        . 'c.workphone as cwphone, c.companyname, c.cardnr as ccardnr, c.cardexp as ccardexp, '
                        . 'cr.* from rentals as r '
                        . 'left join customers as c '
                        . 'on c.drpasportid=r.drpasportid '
                        . 'left join cars as cr on cr.id=r.vehicle '
                        . 'where r.status = "CHECK_OUT" AND r.id=?', array($id));
        $result = $values[0]->rentdays * $values[0]->rate_value;

        $currencies = array(
             'EUR' => 'EUR',
            'LEK' => 'LEK',
        );

        return View::make('pages.check_in', array(
                    'values' => $values,
                    'currencies' => $currencies,
                    'result' => $result
        ));
    }

    public function checkin_update($id) {
        Input::flash();
        $data = [
            "errors" => null
        ];
        $attributes = array(
            'cfirstname' => Lang::get('rentals.cfirstname'),
            'clastname' => Lang::get('rentals.clastname'),
        );
        $rules = array(
            "cfirstname" => "required",
            "clastname" => "required",
        );
        $validation = Validator::make(Input::all(), $rules, array(), $attributes);
        if ($validation->passes()) {

            $data = Rental::find($id);

            $data->drfirstname = Input::get('drfirstname');
            $data->drlastname = Input::get('drlastname');
            $data->drbdate = Input::get('drbdate');
            $data->drlexpdate = Input::get('drlexpdate');
            $data->drliccat = Input::get('drliccat');
            $data->drlicnr = Input::get('drlicnr');
            $data->dr2fname = Input::get('dr2fname');
            $data->dr2lastname = Input::get('dr2lastname');
            $data->draddress = Input::get('draddress');
            $data->dr2bdate = Input::get('dr2bdate');
            $data->dr2licexpdate = Input::get('dr2licexpdate');
            $data->dr2liccat = Input::get('dr2liccat');
            $data->dr2licnr = Input::get('dr2licnr');
            $data->dr2licexpdate = Input::get('dr2licexpdate');
            $data->dr2city = Input::get('dr2city');
            $data->dr2country = Input::get('dr2country');
            $data->fuelin = Input::get('fuelin');
            $data->kmin = Input::get('kmin');
            $data->rentdays = Input::get('nrdays');
            $data->totalpaid = Input::get('result');
            $data->rate = Input::get('rate');
            $data->curency = Input::get('curency');
            $data->agent = Input::get('agency');
            $data->prepaid = Input::get('gurantee');
            $data->curencyg = Input::get('curencyg');
            $data->xkarburant = Input::get('xkarburant');
            $data->xkm = Input::get('xkm');
            $data->xdamage = Input::get('xdamage');
            $data->xlate = Input::get('xlate');
            $data->xclean = Input::get('xclean');
            $data->fuelout = Input::get('fuelout');
            $data->kmout = Input::get('kmout');
            $data->status = 'CHECK_IN';
            $data->user_id = Auth::user()->id;
            $data->save();

            $data2 = Customer::find(Input::get('drpasportid'));
            $data2->drfirstname = Input::get('cfirstname');
            $data2->drlastname = Input::get('clastname');
            $data2->drpasportid = Input::get('drpasportid');
            $data2->draddress = Input::get('caddress');
            $data2->drcity = Input::get('drcity');
            $data2->drcountry = Input::get('drcountry');
            $data2->dremail = Input::get('dremail');
            $data2->drhphone = Input::get('drhphone');
            $data2->workphone = Input::get('workphone');
            $data2->companyname = Input::get('companyname');
            $data2->drbdate = Input::get('drbdate');
            $data2->drlexpdate = Input::get('drlexpdate');
            $data2->drliccat = Input::get('drliccat');
            $data2->drlicnr = Input::get('drlicnr');
            $data2->save();

            $data3 = Car::find(Input::get('vehicle'));
            $data3->Milage = Input::get('milage');
            $data3->fuelin = Input::get('fuelin');

            return Response::json(["redirect_to" => "/"]);
        } else {
            if (Request::ajax()) {
                $response_values = array(
                    'validation_failed' => 1,
                    'errors' => $validation->errors()->toArray(),
                );
                return Response::json($response_values);
            } else {
                $data['errors'] = $validation->errors();
                return View::make('pages.check_in', $data);
            }
        }
    }

    public function upload_patent_rental($drpasportId, $id) {
        $object_patent_file = DB::select('select patent_pic from customers'
                        . ' where drpasportid=?', array($drpasportId));
        $object_image_file = DB::select('select picture from customers'
                        . ' where drpasportid=?', array($drpasportId));
        if ($object_patent_file[0]->patent_pic != NULL) {
            $patent = CloudStorageTools::getImageServingUrl($object_patent_file[0]->patent_pic, ["secure_url" => true]);
        } else {
            $patent = CloudStorageTools::getImageServingUrl('gs://customer_pic/noavatar.png', ["secure_url" => true]);
        }
        if ($object_image_file[0]->picture != NULL) {
            $image = CloudStorageTools::getImageServingUrl($object_image_file[0]->picture, ["secure_url" => true]);
        } else {
            $image = CloudStorageTools::getImageServingUrl('gs://customer_pic/noavatar.png', ["secure_url" => true]);
        }
        return View::make('pages.upload_patent', array(
                    'drpasportId' => $drpasportId,
                    'rentalId' => $id,
                    'patent' => $patent,
                    'image' => $image));
    }

    public function rental_patent($drpasportId, $id) {
        $options = ['gs_bucket_name' => 'national-test.appspot.com'];
        $upload_url = CloudStorageTools::createUploadUrl('/upload_customer_patent', $options);
        $values = DB::select('select * from customers where drpasportid=?', array($drpasportId));
        return View::make('pages.customer_patent', array(
                    'values' => $values,
                    'upload_url' => $upload_url,
                    'rentalId' => $id
        ));
    }
    public function rental_image($drpasportId, $id) {
        $options = ['gs_bucket_name' => 'national-test.appspot.com'];
        $upload_url = CloudStorageTools::createUploadUrl('/upload_customer_image', $options);
        $values = DB::select('select * from customers where drpasportid=?', array($drpasportId));
        return View::make('pages.customer_patent', array(
                    'values' => $values,
                    'upload_url' => $upload_url,
                    'rentalId' => $id
        ));
    }

    /**
     * Upload picture of a customer.
     *
     * @return Response
     */
    public function upload_customer_patent() {
        if (Input::hasFile('uploaded_files')) {
            $drpasportid = Input::get('id_value');
            $rentalId = Input::get('rentalId');
            $options = ['gs' => ['Content-Type' => 'text/plain']];
            $random = str_random(10);
            $ctx = stream_context_create($options);
            if (false == rename($_FILES['uploaded_files']['tmp_name'], 'gs://customer_patent/' . $random . $drpasportid, $ctx)) {
                die('Could not rename.');
            }
            $data = Customer::find($drpasportid);
            $data->patent_pic = 'gs://customer_patent/' . $random . $drpasportid;
            $data->save();

            return Redirect::to('upload_patent_rental/' . $drpasportid . '/' . $rentalId);
        } else {
            $id = Input::get('id_value');
            return Redirect::to('customer_patent/'. $id)->with('message', Lang::get('messages.upload_fail'));
        }
    }
    
        public function upload_customer_image() {
        if (Input::hasFile('uploaded_files')) {
            $drpasportid = Input::get('id_value');
            $rentalId = Input::get('rentalId');
            $options = ['gs' => ['Content-Type' => 'text/plain']];
            $random = str_random(10);
            $ctx = stream_context_create($options);
            if (false == rename($_FILES['uploaded_files']['tmp_name'], 'gs://customer_pic/' . $random . $drpasportid, $ctx)) {
                die('Could not rename.');
            }
            $data = Customer::find($drpasportid);
            $data->picture = 'gs://customer_pic/' . $random . $drpasportid;
            $data->save();

            return Redirect::to('upload_patent_rental/' . $drpasportid . '/' . $rentalId);
        } else {
            $id = Input::get('id_value');
            return Redirect::to('customer_pic/' . $id)->with('message', Lang::get('messages.upload_fail'));
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id) {
//
    }

}
